package com.baymax.test.scene.demo

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ResolveInfo
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newScrollContainer


/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       ProviderTest
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/4/27 14:37
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class OpenBrowserTest :BaseScene() {


    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return  p1.newScrollContainer {
            addButton("test gefo ") {
                val intent = Intent("com.baymax.action.OPEN_BROWSER")
                intent.putExtra("actionUrl", "http://192.168.26.141:8080/index.html") //加载的地址
                intent.putExtra("source", "http://192.168.26.141:8080/index.html") //soure可以不传
                requireActivity().startActivity(intent)
            }
            addButton("updateByCP") {
                val intent = Intent("com.iflytek.xiri.action.LIVE.updateByCP")
                val list: List<ResolveInfo> = requireActivity().packageManager.queryBroadcastReceivers(intent, 0)
                Log.d(TAG, "onCreateView: updateByCP $list")

                kotlin.runCatching {
                    list.forEach {
                        intent.setPackage(it.activityInfo.packageName)
                        Log.d(TAG, "onCreateView: updateByCP $intent")
                        requireActivity().application.sendBroadcast(intent)
                    }
                }
            }


            addButton("open live") {
                val intent = Intent("com.ysten.voice.action.LIVE")
                intent.putExtra("action", "select")
                intent.putExtra("no", "82")
                val list: List<ResolveInfo> = requireActivity().packageManager.queryBroadcastReceivers(intent, 0)
                Log.d(TAG, "onCreateView: open live $list")
                kotlin.runCatching {
                    requireActivity().application.sendBroadcast(intent)
                }
            }
            addButton("open live with package") {
                val intent = Intent("com.ysten.voice.action.LIVE")
                intent.putExtra("action", "select")
                intent.putExtra("no", "82")
                intent.setPackage("tv.icntv.ott.soft.ypjs")
                val list: List<ResolveInfo> = requireActivity().packageManager.queryBroadcastReceivers(intent, 0)
                Log.d(TAG, "onCreateView: open live $list")
                kotlin.runCatching {
                    requireActivity().application.sendBroadcast(intent)
                }

            }
            addButton("open xiri live ") {
                val intent = Intent("com.iflytek.xiri.action.LIVE")
                intent.setType("xiri/com.iflytek.xiri.ysten.livechannels")
                intent.putExtra("action", "select")
                intent.putExtra("no", "82")
                val list: List<ResolveInfo> = requireActivity().packageManager.queryBroadcastReceivers(intent, 0)
                Log.d(TAG, "onCreateView: open live $list")
                kotlin.runCatching {
                    requireActivity().application.sendBroadcast(intent)
                }
            }
            addButton("open xiri live with package") {
                val intent = Intent("com.iflytek.xiri.action.LIVE")
                intent.setType("xiri/com.iflytek.xiri.ysten.livechannels")
                intent.setPackage("tv.icntv.ott.soft.ypjs")
                intent.putExtra("action", "select")
                intent.putExtra("no", "82")
                val list: List<ResolveInfo> = requireActivity().packageManager.queryBroadcastReceivers(intent, 0)
                Log.d(TAG, "onCreateView: open live $list")
                kotlin.runCatching {
                    requireActivity().application.sendBroadcast(intent)
                }
            }



        }.also {
//            requireActivity().registerReceiver(ob , IntentFilter("com.iflytek.xiri.action.LIVE.updateByCP"))
//            lifecycle.addObserver(object : LifecycleEventObserver{
//                override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
//                    if (event == Lifecycle.Event.ON_DESTROY){
//                        requireActivity().unregisterReceiver(ob)
//                    }
//                }
//            })
        }


    }
}